package com.gdhuarui.controller;

import com.gdhuarui.common.NumberHelper;
import com.gdhuarui.pojo.*;
import com.gdhuarui.service.IndexService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;

    /**
     * 获取系统配置信息
     *
     * @return
     */
    public HashMap<String, Config> config() {
        ArrayList<Config> configArrayList = indexService.getConfigs();
        // List转Map，基于JDK1.8的Stream API和函数式接口实现，同时还可去重
        HashMap<String, Config> configHashMap = (HashMap<String, Config>) configArrayList.stream().collect(Collectors.toMap(Config::getKey, Function.identity()));
        return configHashMap;
    }

    /**
     * 首页
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/")
    public String home (Model model){
        ArrayList<News> industryNews = indexService.getNewsByCatalog("行业资讯");
        ArrayList<News> companyNews = indexService.getNewsByCatalog("公司新闻");
        ArrayList<Project> projects = indexService.getProjectsLimitSix();
        ArrayList<Partner> partners = indexService.getPartnerslimitSix();
        model.addAttribute("configs", config());
        model.addAttribute("industryNews", industryNews);
        model.addAttribute("companyNews", companyNews);
        model.addAttribute("projects", projects);
        model.addAttribute("partners", partners);
        return "index/index.html";
    }

    /**
     * 首页
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/Index/index")
    public String index (Model model){
        ArrayList<News> industryNews = indexService.getNewsByCatalog("行业资讯");
        ArrayList<News> companyNews = indexService.getNewsByCatalog("公司新闻");
        ArrayList<Project> projects = indexService.getProjectsLimitSix();
        ArrayList<Partner> partners = indexService.getPartnerslimitSix();
        model.addAttribute("configs", config());
        model.addAttribute("industryNews", industryNews);
        model.addAttribute("companyNews", companyNews);
        model.addAttribute("projects", projects);
        model.addAttribute("partners", partners);
        return "index/index.html";
    }

    /**
     * 关于我们 - 公司简介
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/About/about")
    public String about (Model model){
        model.addAttribute("configs", config());
        return "index/about.html";
    }

    /**
     * 关于我们 - 经营范围
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/About/scope")
    public String scope (Model model){
        model.addAttribute("configs", config());
        return "index/scope.html";
    }

    /**
     * 关于我们 - 企业文化
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/About/civilization")
    public String civilization (Model model){
        model.addAttribute("configs", config());
        return "index/civilization.html";
    }

    /**
     * 关于我们 - 人才体系
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/About/setup")
    public String setup (Model model){
        model.addAttribute("configs", config());
        return "index/setup.html";
    }

    /**
     * 关于我们 - 合作伙伴
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/About/partner")
    public String partner (Model model){
        model.addAttribute("configs", config());
        return "index/partner.html";
    }

    /**
     * 联系我们 - 联系方式
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/Contact/contact")
    public String contact (Model model){
        model.addAttribute("configs", config());
        return "index/contact.html";
    }

    /**
     * 联系我们 - 在线留言
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/Contact/online")
    public String online (Model model){
        model.addAttribute("configs", config());
        return "index/online.html";
    }

    /**
     * 人才招聘 - 人才中心
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/Talent/talent")
    public String talent (Model model){
        model.addAttribute("configs", config());
        return "index/talent.html";
    }

    /**
     * 新闻中心 - 行业资讯
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/News/industry")
    public String industry (@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "5") int pageSize, Model model) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<News> pageInfo = new PageInfo<>(indexService.getNewsByCatalogPage("行业资讯"));
        model.addAttribute(pageInfo);
        model.addAttribute("configs", config());
        return "index/industry.html";
    }

    /**
     * 新闻中心 - 公司新闻
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/News/company")
    public String company (@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "5") int pageSize, Model model) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<News> pageInfo = new PageInfo<>(indexService.getNewsByCatalogPage("公司新闻"));
        model.addAttribute(pageInfo);
        model.addAttribute("configs", config());
        return "index/company.html";
    }

    /**
     * 新闻详情
     *
     * @param model
     * @param newsId
     * @return
     */
    @GetMapping(value = "/News/newsDetail/id/{newsId}")
    public String newsDetail (Model model, @PathVariable String newsId){
        int id = 0;
        if (NumberHelper.isInteger(newsId)) {
            id = Integer.parseInt(newsId);
            News news = indexService.getNewsById(id);
            if (null == news) {
                return "error.html";
            } else {
                model.addAttribute("news", news);
                model.addAttribute("configs", config());
            }
        }else{
            return "error.html";
        }
        return "index/news_detail.html";
    }

    /**
     * 项目案例
     *
     * @param pageNo
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping(value = "/Project/projects")
    public String projects (@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "8") int pageSize, Model model) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Project> pageInfo = new PageInfo<>(indexService.getProjectsPage());
        model.addAttribute(pageInfo);
        model.addAttribute("configs", config());
        return "index/projects.html";
    }

    /**
     * 下载中心
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/Download/download")
    public String download (@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "3") int pageSize, Model model) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Product> pageInfo = new PageInfo<>(indexService.getProductsPage());
        model.addAttribute(pageInfo);
        model.addAttribute("configs", config());
        return "index/download.html";
    }

    /**
     * 产品下载
     *
     * @param model
     * @param productId
     * @return
     */
    @GetMapping(value = "/Download/product/id/{productId}")
    public String product (Model model, @PathVariable String productId){
        int id = 0;
        if (NumberHelper.isInteger(productId)) {
            id = Integer.parseInt(productId);
            Product product = indexService.getProductById(id);
            if (null == product) {
                return "error.html";
            } else {
                model.addAttribute("product", product);
            }
        }else{
            return "error.html";
        }
        return "index/product.html";
    }
}
